<?php
namespace WebApper\Shortcode;

/*
 * [user_settings id='UserSettingsBuilder']
 *
 */
class UserSettings extends \WebApper\Shortcode {
	
    /**
     * Define shortcode properties
     *
     */
	protected $shortcode = 'user_settings';
	protected $defaults = array(
		'id' => null,	// The unique ID to use for the shortcode. Default: none. Required.
	);

    /**
     * Handles the add post shortcode
     *
     * @param array $atts
     */
    public function shortcode( $atts ) {

		// Get shortcode attributes
			extract( shortcode_atts( $this->defaults, $atts ) );

		// Check for required shortcode attributes
			$msg = $this->has_req_attrs( $atts );
			if ( $msg !== true )
				return $msg;
			
		// Ouput HTML
		$userID = get_current_user_id();
		?>
        <form id="userSettings" name="userSettings" class="form-horizontal">
            <input type="hidden" id="action" name="action" value="web_apper<?php echo $this->shortcode; ?>">
            <?php wp_nonce_field('WebApperAwesomeness!87','web_apper_nonce'); ?>
            <div class="accordion" id="emailSettings">
                <div class="accordion-group">
                    <div class="accordion-heading">
                        <a class="accordion-toggle" data-toggle="collapse" data-parent="#emailSettings" href="#collapseOne">
                            Emails
                        </a>
                    </div>
                    <div id="collapseOne" class="accordion-body collapse in">
                        <div class="accordion-inner">
                        
                            <div class="control-group">
                                <label class="control-label" for="emailAddress">Email Address</label>
                                <div class="controls">
                                    <input type="text" id="emailAddress" name="emailAddress" placeholder="Email Address" value="<?php echo get_user_meta( $userID, 'emailAddress', true ); ?>">
                                </div>
                                
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="emailUserName">Email User Name</label>
                                <div class="controls">
                                    <input type="text" id="emailUserName" name="emailUserName" placeholder="Email User Name" value="<?php echo get_user_meta( $userID, 'emailUserName', true ); ?>">
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="emailPassword">Password</label>
                                <div class="controls">
                                    <input type="text" id="emailPassword" name="emailPassword" placeholder="Password" value="<?php echo get_user_meta( $userID, 'emailPassword', true ); ?>">
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="emailServer">Server</label>
                                <div class="controls">
                                    <input type="text" id="emailServer" name="emailServer" placeholder="Server" value="<?php echo get_user_meta( $userID, 'emailServer', true ); ?>">
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="emailPort">Port</label>
                                <div class="controls">
                                     <input type="text" id="emailPort" name="emailPort" placeholder="Port" value="<?php echo get_user_meta( $userID, 'emailPort', true ); ?>">
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="emailSSL">SSL</label>
                                <div class="controls">
                                    <select id="emailSSL" name="emailSSL">
                                    	<option value="true"<?php if ( get_user_meta( $userID, 'emailSSL', true ) == 'true' ) { echo ' selected'; } ?>>true</option>
                                    	<option value="false"<?php if ( get_user_meta( $userID, 'emailSSL', true ) == 'false' ) { echo ' selected'; } ?>>false</option>
                                    </select>
                                </div>
                            </div>
                            <div class="control-group">
                                <label class="control-label" for="emailSignature">Signature</label>
                                <div class="controls">
                                    <textarea id="emailSignature" name="emailSignature"><?php echo get_user_meta( $userID, 'emailSignature', true ); ?></textarea>
                                </div>
                            </div>
                            
                        </div>
                    </div>
                </div>
            </div>
        </form>
        <button id="submitSettings" name="submitSettings" data-loading-text="Saving..." class="btn btn-primary btn-large pull-right">Save Settings</button>

		<script type="text/javascript">
			
			// Store Commonly manipulated DOM nodes as objects
			nForm = jQuery('#userSettings');
			nFormSubmit = jQuery('#submitSettings')

            jQuery(document).ready(function($) {

                // Add a click handler to the modal action button for form submission
                nFormSubmit.on("click", function() {
                    nFormSubmit.button('loading') ; // Give modal submit button a loading state
                    var data = nForm.serialize();  // serialize form data
                    $.post("<?php echo admin_url('admin-ajax.php'); ?>", data, function(response) {	// Submit form via ajax
						var result = $.parseJSON(response);  // Parse response
console.log(result);
                        nFormSubmit.button('reset');  // Remove loading state from modal submit button
                        nForm.prepend(result.htmlalert);  // Show and alert
                    });
                });

            });
        </script>
        <?php
	}

    /**
     * Handles ajax requests
     *
     * @since 1.0
     */
	public function sendAjaxResponse() {
//		if ( isset( $_POST['userSettings'] ) ) :
			check_ajax_referer( 'WebApperAwesomeness!87', 'web_apper_nonce' );  // Verify the security nonce
			echo json_encode( $this->update_settings() );
//		endif;
		die();  // Prevents ajax from returning a '0'
	}

    /**
     * Update uer settings
     *
     * @since 1.0
     */
	public function update_settings() {
		$userID = get_current_user_id();
		$setting1 = update_user_meta( $userID, 'emailAddress', $_POST['emailAddress'] );
		$settingName = update_user_meta( $userID, 'emailUserName', $_POST['emailUserName'] );
		$setting2 = update_user_meta( $userID, 'emailPassword', $_POST['emailPassword'] );
		$setting3 = update_user_meta( $userID, 'emailServer', $_POST['emailServer'] );
		$setting4 = update_user_meta( $userID, 'emailPort', $_POST['emailPort'] );
		$setting5 = update_user_meta( $userID, 'emailSSL', $_POST['emailSSL'] );
		$setting6 = update_user_meta( $userID, 'emailSignature', $_POST['emailSignature'] );
		return $this->sendResponse( 'Settings updated.', 'alert-success', 'Hurray!', true );  // Send Response
	}

}

$initialize = new UserSettings(); 

?>